<?php

include_once '../common.php';

if (!is_file('config.php')) {
  copy('config.template','config.php');
  chmod('config.php',777);
  warn('Copied config.template -> config.php, be sure to chmod config.php 755 again');
 }

include 'config.php';
if (isset($_REQUEST['submit'])) {
  $fh = fopen('config.php','w');
  fwrite($fh,'<?php' . "\n");
  foreach ($CONFIG as $k => $v) {
    fwrite($fh,'$CONFIG["' . htmlspecialchars($k) . '"] = "');
    if (isset($_REQUEST[$k])) {
      $val = $_REQUEST[$k];
    } else {
      $val = $v;
    }
    $CONFIG[$k] = $val;
    fwrite($fh,htmlspecialchars($_REQUEST[$k]));
    fwrite($fh,"\";\n");
  }
  fwrite($fh,'?>' . "\n");
  fclose($fh);
  note('Updated config.php');
 }


echo '<h1>Config</h1>';

echo '<form type="post" action="' . $_SERVER['PHP_SELF'] . '">';
foreach ($CONFIG as $k => $v) {
  echo '<p>';
  echo option($k);
  echo '</p>';
}
echo '<input type="submit" name="submit" value="Change"/>';
echo '</form>';

echo '<form action="done.php">';
echo '<input type="submit" name="done" value="Done"/>';
echo '</form>';

?>